import {defineStore} from "pinia";
import {appConfigType} from "@/types/component/core";
import app from "@/app";

//运行栈
export default defineStore({
    id: "task",
    state: () => ({
        //app注册表
        appList: [] as appConfigType[] | [],
        //app运行栈
        taskList: [] as appConfigType[] | [],
        //当前活跃的app
        activeTask: null as appConfigType | null
    }),
    getters: {},
    actions: {
        getTaskIdx(name: string): number {
            let idx = this.taskList.findIndex((el: appConfigType) => {
                return el.name === name
            })
            return idx
        },
        GET_APP(name: string): appConfigType | null {
            let app: appConfigType | null = null
            if (this.appList && this.appList.length > 0) {
                app = this.appList.find((el) => el.name === name)
            }
            if (app) {
                return app
            } else {
                return null
            }

        },
        //打开窗口
        OPEN_APP(name: string) {
            // @ts-ignore
            let list: appConfigType[] | [] = this.appList
            let app: appConfigType | undefined = undefined
            //当前这个app是否在使用
            app = this.taskList.find((el: appConfigType) => {
                return el.name === name
            })
            //如果当前app没有在使用
            // @ts-ignore
            if (!app && list && list.length > 0) {

                console.log('open')
                // @ts-ignore
                let openApp: appConfigType | undefined = undefined

                openApp = list.find((el: appConfigType) => {
                    return el.name == name
                })
                console.log(openApp)
                if (openApp) {
                    // @ts-ignore
                    this.taskList.push({...openApp})
                    this.activeTask = {...openApp}
                    this.SHOW_APP(name)
                }
                console.log(this.taskList)
            } else if (app) {
                this.activeTask = app
                this.SHOW_APP(name)
            }

        },
        //获取所有APP列表
        async GET_APPLIST() {
            this.appList = await app.getAppList()
        },
        //让某个APP处于活跃状态
        ACTIVE_APP(name: string) {
            if (this.activeTask && this.activeTask.name !== name) {
                let app = this.taskList.find((el: appConfigType) => {
                    return el.name === name
                })
                if (app) {
                    this.activeTask = app
                    this.SHOW_APP(name)
                }
            }
        },
        //判断某个APP是否活跃
        APP_IS_ACTIVE(name: string): boolean {
            if (this.activeTask) {
                return this.activeTask.name === name
            } else {
                return false
            }
        },

        WINDOW_NAV_TO(name: string, path: string) {
            let idx = this.getTaskIdx(name)
            if (idx !== -1) {
                this.taskList[idx].currentPath = path
                this.activeTask = this.taskList[idx]
            }
        },
        //关闭某个app
        CLOSE_APP(name: string) {
            console.log(name)
            let idx = this.getTaskIdx(name)
            if (this.activeTask && name === this.activeTask.name && this.taskList.length > 1) {
                console.log('tp1')
                if (this.taskList[idx + 1]) {
                    this.activeTask = this.taskList[idx + 1]
                    this.taskList.splice(idx, 1)
                } else if (this.taskList[idx - 1]) {
                    this.activeTask = this.taskList[idx - 1]
                    this.taskList.splice(idx, 1)
                }
            } else if (this.activeTask && name === this.activeTask.name && this.taskList.length == 1) {
                console.log('tp2')
                this.taskList.splice(idx, 1)
                this.activeTask = null
            }
        },
        //隐藏app
        HIDDEN_APP(name: string) {
            let idx = this.getTaskIdx(name)
            if (idx !== -1) {
                this.taskList[idx].windowHidden = true
            }
        },
        //展示app
        SHOW_APP(name: string) {
            let idx = this.getTaskIdx(name)
            if (idx !== -1) {
                this.taskList[idx].windowHidden = false
            }
        },
        //全屏
        FULL_APP(name: string) {
            let idx = this.getTaskIdx(name)
            if (idx !== -1) {
                this.taskList[idx].isFull = {
                    windowX: <number>this.taskList[idx].windowX,
                    windowY: <number>this.taskList[idx].windowY,
                    windowHeight: this.taskList[idx].windowHeight,
                    windowWidth: this.taskList[idx].windowWidth,
                }
                this.taskList[idx].windowY = 30
                this.taskList[idx].windowX = 0
                this.taskList[idx].windowWidth = document.body.clientWidth
                this.taskList[idx].windowHeight = document.body.clientHeight - 30
            }
        },
        //取消全屏
        CANCEL_FULL(name: string) {
            console.log('cancel')
            let idx = this.getTaskIdx(name)
            if (idx !== -1 && this.taskList && this.taskList[idx].isFull) {
                // @ts-ignore
                this.taskList[idx].windowY = this.taskList[idx].isFull.windowY
                // @ts-ignore
                this.taskList[idx].windowX = this.taskList[idx].isFull.windowX
                // @ts-ignore
                this.taskList[idx].windowWidth = this.taskList[idx].isFull.windowWidth
                // @ts-ignore
                this.taskList[idx].windowHeight = this.taskList[idx].isFull.windowHeight
                this.taskList[idx].isFull = null

            }
        }
    }
});